﻿@using Volo.Abp.AspNetCore.Mvc.UI.Layout
@using Volo.Abp.UI.Navigation
@inject IPageLayout PageLayout
@model ApplicationMenuItem
@{
    var cssClass = string.IsNullOrEmpty(Model.CssClass) ? string.Empty : Model.CssClass;
    var disabled = Model.IsDisabled ? "disabled" : string.Empty;
    var url = string.IsNullOrEmpty(Model.Url) ? "#" : Url.IsLocalUrl(Model.Url) ? Url.Content(Model.Url.EnsureStartsWith('~')) : Model.Url;
    var customComponentType = Model.GetComponentTypeOrDefault();
    var isActive = Model.Name == PageLayout.Content.MenuItemName ? "active" : string.Empty;
}
@if (Model.IsLeaf)
{
    if (customComponentType != null && typeof(ViewComponent).IsAssignableFrom(customComponentType))
    {
        @(await Component.InvokeAsync(customComponentType))
    }
    else if (Model.Url != null)
    {
        var useLocalePrefix = Model.IsSupportsLocalePrefix();
        <a class="dropdown-item @cssClass @disabled @isActive" href="@url" add-culture="@useLocalePrefix" target="@Model.Target" id="Model.ElementId">
            @if (Model.Icon != null)
            {
                <i class="@Model.Icon"></i>
            }
            @Model.DisplayName
        </a>
    }
}
else
{
    <div class="dropdown-submenu">
        @if (customComponentType != null && typeof(ViewComponent).IsAssignableFrom(customComponentType))
        {
            @(await Component.InvokeAsync(customComponentType))
        }
        else
        {
            <a role="button" class="dropdown-item dropdown-toggle" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                @if (Model.Icon != null)
                {
                    <i class="@Model.Icon"></i>
                }
                @Model.DisplayName
            </a>
            <div class="dropdown-menu">
                @foreach (var childMenuItem in Model.Items)
                {
                    @await Html.PartialAsync("Components/Menu/_MenuItem", childMenuItem)
                }
            </div>
        }
    </div>
}
